package com.hj.ct.fragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.hj.ct.R;
import com.hj.ct.activity.LoginActivity;
import com.hj.ct.adapter.SpinnerAdapter;
import com.hj.ct.logic.GatherDataLogic;
import com.hj.ct.logic.LogicActions;
import com.hj.ct.parse.Report2Data;

public class BarChart2Fragment extends BaseFragment implements OnItemSelectedListener {

	protected BarChart mChart1;
	private Typeface tf;
	private View loadingLayout;
	private Spinner time;
	private long currentTime = System.currentTimeMillis();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");
		View view = inflater.inflate(R.layout.fragment_chart2, null);
		mChart1 = (BarChart) view.findViewById(R.id.chart2);
		loadingLayout = view.findViewById(R.id.loading);
		time = (Spinner) view.findViewById(R.id.time_long);
		SpinnerAdapter adapter = new SpinnerAdapter(getActivity());
		adapter.setList(Arrays.asList("实时值", "小时均值", "日均值"));
		time.setAdapter(adapter);
		time.setOnItemSelectedListener(this);
		initBarChart(mChart1);
		loading();
		GatherDataLogic.getInstance().getChart2(this, currentTime, 1);
		return view;
	}
	public void setCurrentTime(long time){
		this.currentTime = time;
		this.time.setSelection(0);
		loading();
		GatherDataLogic.getInstance().getChart2(this, currentTime, 1);
	}
	private void loading() {
		loadingLayout.setVisibility(View.VISIBLE);
	}

	private void closeLoading() {
		loadingLayout.setVisibility(View.GONE);
	}

	private void initBarChart(BarChart barChart) {
		barChart.setDrawBarShadow(false);
		barChart.setDrawValueAboveBar(true);
		barChart.setDescription("粉尘噪声超标率");
		barChart.setNoDataTextDescription("暂无数据");
		barChart.setMaxVisibleValueCount(60);
		barChart.setTouchEnabled(true);
		barChart.setPinchZoom(true);
		barChart.setDrawGridBackground(false);

		XAxis xl = barChart.getXAxis();
		xl.setPosition(XAxisPosition.BOTTOM);
		xl.setTypeface(tf);
		xl.setDrawAxisLine(true);
		xl.setDrawGridLines(true);
		xl.setGridLineWidth(0.3f);

		YAxis leftAxis = mChart1.getAxisLeft();
		leftAxis.setTypeface(tf);
		leftAxis.setTextColor(ColorTemplate.getHoloBlue());
		leftAxis.setAxisMaxValue(2.0f);
		leftAxis.setDrawGridLines(true);

		YAxis rightAxis = mChart1.getAxisRight();
		rightAxis.setTypeface(tf);
		rightAxis.setTextColor(Color.RED);
		rightAxis.setAxisMaxValue(2.0f);
		rightAxis.setStartAtZero(false);
		rightAxis.setAxisMinValue(0);
		rightAxis.setDrawGridLines(false);

		Legend l = barChart.getLegend();
		l.setPosition(LegendPosition.RIGHT_OF_CHART_INSIDE);
		l.setForm(LegendForm.SQUARE);
		l.setFormSize(9f);
		l.setTextSize(11f);
		l.setXEntrySpace(4f);
	}

	private void setData(List<Report2Data> datas, int position) {
		if (datas == null) {
			return;
		}
		ArrayList<String> xVals = new ArrayList<String>();
		ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
		ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
		ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
		ArrayList<BarEntry> yVals4 = new ArrayList<BarEntry>();
		ArrayList<BarEntry> yVals5 = new ArrayList<BarEntry>();
		int count = datas.size();
		for (int i = 0; i < count; i++) {
			Report2Data report2Data = datas.get(i);
			xVals.add("设备" + report2Data.getDeviceID());
			yVals1.add(new BarEntry(report2Data.getDust1(), i));
			yVals2.add(new BarEntry(report2Data.getDust2(), i));
			yVals3.add(new BarEntry(report2Data.getDust3(), i));
			yVals4.add(new BarEntry(report2Data.getNoiseDay(), i));
			yVals5.add(new BarEntry(report2Data.getNoiseNight(), i));

		}

		BarDataSet set1 = new BarDataSet(yVals1, "粉尘>0.3mg/m3");
		set1.setAxisDependency(AxisDependency.LEFT);
		set1.setColor(0xFF3CB371);
		set1.setValueTextColor(ColorTemplate.getHoloBlue());
		set1.setBarSpacePercent(10f);
		BarDataSet set2 = new BarDataSet(yVals2, "粉尘>0.5mg/m3");
		set2.setAxisDependency(AxisDependency.LEFT);
		set2.setColor(0xFF00FA9A);
		set2.setValueTextColor(ColorTemplate.getHoloBlue());
		set2.setBarSpacePercent(10f);

		BarDataSet set3 = new BarDataSet(yVals3, "粉尘>1.0mg/m3");
		set3.setAxisDependency(AxisDependency.LEFT);
		set3.setColor(0xFF48D1CC);
		set3.setValueTextColor(ColorTemplate.getHoloBlue());
		set3.setBarSpacePercent(10f);

		BarDataSet set4 = new BarDataSet(yVals4, "日间噪声超标比率 ");
		set4.setAxisDependency(AxisDependency.RIGHT);
		set4.setColor(0xFFCD5C5C);
		set4.setValueTextColor(Color.RED);
		set4.setBarSpacePercent(10f);
		BarDataSet set5 = new BarDataSet(yVals5, "夜间噪声超标比率");
		set5.setAxisDependency(AxisDependency.RIGHT);
		set5.setColor(0xFFF08080);
		set5.setValueTextColor(Color.RED);
		set4.setBarSpacePercent(10f);

		ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
		dataSets.add(set1);
		dataSets.add(set2);
		dataSets.add(set3);
		dataSets.add(set4);
		dataSets.add(set5);

		BarData data = new BarData(xVals, dataSets);
		data.setValueTextSize(10f);
		data.setValueTypeface(tf);

		mChart1.setData(data);
		mChart1.notifyDataSetChanged();
		mChart1.invalidate();
	}

	private List<Report2Data> report1Data;

	public void handlerUI(int action, int code, Object... obj) {
		closeLoading();
		if (action == LogicActions.REPORT2_DATA_SUCCESS) {
			report1Data = (List<Report2Data>) obj[0];
			if (report1Data == null)
				return;
			setData(report1Data, 0);
		} else if (action == LogicActions.REPORT2_DATA_ERROR) {
			toast(obj[0] + "");
		} else if (action == LogicActions.NEED_TO_LOGIN) {
			showReLoginDialog();
		}

	}

	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		loading();
		GatherDataLogic.getInstance().getChart2(this, currentTime, position + 1);
	}

	public void onNothingSelected(AdapterView<?> parent) {

	}

}
